package blog.service.impl;

import blog.mapper.BlogMapper;
import blog.pojo.Blog;
import blog.pojo.ResultVo;
import blog.service.BlogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HuLin
 * @date
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;


    @Override
    public ResultVo selectBlogByCid(int cid) {
//      创建条件构造器
	  QueryWrapper queryWrapper = new QueryWrapper();
//	  参数1：数据库字段
//	  参数2：属性值
	  queryWrapper.eq("cid",cid);
	  List<Blog> blogList = blogMapper.selectList(queryWrapper);
	  if(blogList!=null && blogList.size()>0){
		ResultVo success = new ResultVo(0, "ByCid_success", blogList);
		return success;
	  }
	  return new ResultVo(100,"ByCid_fail",null);
    }

    @Override
    public ResultVo selectBlogByBid(int bid) {
	  Blog blog = blogMapper.selectBlogDetail(bid);
	  if (blog != null){
//	      每当查询成功，hit++
		blog.setHit(blog.getHit()+1);
		blogMapper.updateById(blog);

	      return new ResultVo(0,"ByBid_success",blog);
	  }
	  return new ResultVo(100,"ByBid_fail",null);
    }

    @Override
    public ResultVo addLikes(int bid) {
//        1.根据bid
	  Blog blog = blogMapper.selectById(bid);
//	  2.likes++
	  blog.setLikes(blog.getLikes()+1);
//	  3.修改blog
	  int i = blogMapper.updateById(blog);
	  if(i>0){
	      return new ResultVo(0,"Likes success");
	  }
	  return new ResultVo(100,"Likes fail");
    }

}
